const {
	app,
	BrowserWindow,
	ipcMain,
	dialog,
	ipcRenderer,
} = require("electron");

let mainWindow;

function createWindow() {
	mainWindow = new BrowserWindow({
		width: 800,
		height: 600,
		webPreferences: {
			nodeIntegration: true,
			webSecurity: false,
		},
	});

	mainWindow.loadFile("./src/index.html");

	mainWindow.webContents.openDevTools();

	mainWindow.on("closed", function() {
		mainWindow = null;
	});
}

app.on("ready", createWindow);

app.on("window-all-closed", function() {
	if (process.platform !== "darwin") app.quit();
});

app.on("activate", function() {
	if (mainWindow === null) createWindow();
});

ipcMain.on("open-directory-dialog", function(event, type) {
	dialog.showOpenDialog(
		{
			properties: ["openDirectory"],
		},
		function(files) {
			if (files) {
				event.sender.send("xcx-select-folder", files);
			}
		},
	);
});

ipcMain.on(`open-message-box`, (event, params = {}) => {
	params = Object.assign(
		{
			type: "warning",
			buttons: ["取消", "确定"],
			defaultId: 1,
			title: "提示",
		},
		params,
	);
	dialog.showMessageBox(params, (response, checkboxChecked) => {
		event.sender.send("message-box-response", response, checkboxChecked);
	});
});

